package cs.sxh.controller;

import cs.sxh.pojo.Menu;
import cs.sxh.pojo.User;
import cs.sxh.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author songxh
 * @create 2022-03-23-12:32
 */
@Controller
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    @Qualifier("MenuServiceImpl")
    private MenuService menuService;


    @RequestMapping("/home")
    public String homePage(HttpSession session , Model model){
        User user = (User) session.getAttribute("usermsg");
        List<Menu> menus = menuService.queryMenuByRole(user.getU_role());

        //用set去重，但set是无序的，所以使用LinkedHashSet
        Set<Menu> set=new LinkedHashSet<>();
        for (Menu menu : menus) {
            set.add(menu);
        }
        List<Menu> list=new ArrayList<>();
        List<Menu> list1=new ArrayList<>();

        for (Menu menu : set) {
            if (menu.getM_parent()==0){
                list.add(menu);
            }else {
                list1.add(menu);
            }
        }
//        for (Menu menu : menus) {
//            if (menu.getM_parent()==0){
//                list.add(menu);
//            }else {
//                list1.add(menu);
//            }
//        }
        session.setAttribute("mlist",list);
        session.setAttribute("mlist1",list1);

        return "homePage/home";
    }

    @RequestMapping("/menuManger")
    public String menuManager(int id){
        if (id==3){
            return "redirect:/user/userinfo";
        }else if (id==4){
            return "menu/password";
        }else if (id==5){
            return "redirect:/user/users";
        }

        //此句话没啥用，也就是为了让它不报错
        return "homePage/home";
    }


}
